package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.RedisConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    @Override
    @CacheEvict(cacheNames = RedisConstant.SETMEAL_KEY, allEntries = true)
    public void addSetmeal(SetmealDTO setmealDTO) {

        Setmeal setmeal = Setmeal.builder()
                                .status(StatusConstant.ENABLE)
                                .build();
        // 同名属性赋值
        BeanUtils.copyProperties(setmealDTO, setmeal);

        // 添加套餐信息
        setmealMapper.addSetmeal(setmeal);

        // id回显
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish:setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        //添加套餐菜品信息
        setmealDishMapper.addSetmealDish(setmealDishes);

    }

    /**
     * 分页查询套餐信息
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        // 获取页码信息
        int page = setmealPageQueryDTO.getPage();
        int pageSize = setmealPageQueryDTO.getPageSize();
        // 分页插件
        PageHelper.startPage(page, pageSize);
        List<SetmealVO> setmeals = setmealMapper.queryPage(setmealPageQueryDTO);
        PageInfo<SetmealVO> setmealPageInfo = new PageInfo<>(setmeals);
        //获取分页后的信息
        long total = setmealPageInfo.getTotal();
        List<SetmealVO> list = setmealPageInfo.getList();

        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRecords(list);

        return pageResult;
    }

    /**
     * 启售/停售套餐
     * @param status
     * @param id
     */
    @Override
    @CacheEvict(cacheNames = RedisConstant.SETMEAL_KEY, allEntries = true)
    public void enableOrDisable(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                                .status(status)
                                .id(id)
                                .build();

        List<Long> dishIds = setmealDishMapper.getDishIdById(id);
        //判断套餐内是否包含未启售的菜品，若包含则不能启售套餐
        for (Long dishId : dishIds) {
            int dishStatus = dishMapper.getEnableById(dishId);
            if(dishStatus == StatusConstant.DISABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }

        setmealMapper.update(setmeal);
    }

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @Transactional
    @Override
    public SetmealVO getById(Long id) {

        SetmealVO setmealVO = SetmealVO.builder()
                                        .id(id)
                                        .build();

        //根据id查询套餐信息
        Setmeal setmeal = setmealMapper.getbyId(id);
        //同名属性赋值
        BeanUtils.copyProperties(setmeal, setmealVO);

        //根据id查询套餐关联的菜品信息
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);

        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /**
     * 修改套餐信息
     * @param setmealDTO
     */
    @Transactional
    @Override
    @CacheEvict(cacheNames = RedisConstant.SETMEAL_KEY, allEntries = true)
    public void update(SetmealDTO setmealDTO) {

        Setmeal setmeal = Setmeal.builder().build();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        // 更新套餐关联菜品信息，采用先删除后添加的方式
        Long setmealId = setmealDTO.getId();
        setmealDishMapper.deletebyId(setmealId);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish:setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        //添加套餐菜品信息
        setmealDishMapper.addSetmealDish(setmealDishes);
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Transactional
    @Override
    @CacheEvict(cacheNames = RedisConstant.SETMEAL_KEY, allEntries = true)
    public void deleteBatch(List<Long> ids) {
        // 判断套餐是否启售
        for (Long id : ids) {
            int status = setmealMapper.getStatusById(id);
            if(status == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        // 删除套餐信息
        setmealMapper.deleteBatch(ids);
        // 删除套餐关联的菜品信息
        setmealDishMapper.deleteByIds(ids);

    }

    /**
     * 根据分类id查询套餐列表
     * @param categoryId
     * @return
     */
    @Override
    @Cacheable(cacheNames = RedisConstant.SETMEAL_KEY, key = "#categoryId")
    public List<Setmeal> getSetmealsByCategoryId(Long categoryId) {
        List<Setmeal> setmeals = setmealMapper.getSetmealsByCategoryId(categoryId);
        return setmeals;
    }

    /**
     * 根据套餐id查询包含菜品信息
     * @param id
     * @return
     */
    @Override
    @Cacheable(cacheNames = RedisConstant.SETMEAL_DISH_KEY, key = "#id")
    public List<DishItemVO> getDishBySetmealId(Long id) {
        List<DishItemVO> dishItemVOS = setmealMapper.getDishBySetmealId(id);
        return dishItemVOS;
    }
}
